﻿namespace UnityEditor
{
    using System;
    using UnityEngine;

    internal class TreePainter
    {
        public static float brushSize = 40f;
        public static int selectedTree;
        public static float spacing = 0.8f;
        public static Terrain terrain;
        public static float treeColorAdjustment = 0.4f;
        public static float treeHeight = 1f;
        public static float treeHeightVariation = 0.1f;
        public static float treeWidth = 1f;
        public static float treeWidthVariation = 0.1f;

        private static Color GetTreeColor()
        {
            Color color = (Color) (Color.white * UnityEngine.Random.Range((float) 1f, (float) (1f - treeColorAdjustment)));
            color.a = 1f;
            return color;
        }

        private static float GetTreeHeight()
        {
            return (treeHeight * UnityEngine.Random.Range((float) (1f - treeHeightVariation), (float) (1f + treeHeightVariation)));
        }

        private float GetTreePlacementSize(float treeCount)
        {
            return TerrainInspectorUtil.GetTreePlacementSize(terrain.terrainData, selectedTree, spacing, treeCount);
        }

        private static float GetTreeWidth()
        {
            return (treeWidth * UnityEngine.Random.Range((float) (1f - treeWidthVariation), (float) (1f + treeWidthVariation)));
        }

        public static void PlaceTrees(float xBase, float yBase)
        {
            if (terrain.terrainData.treePrototypes.Length != 0)
            {
                selectedTree = Mathf.Min(TerrainInspectorUtil.GetPrototypeCount(terrain.terrainData) - 1, selectedTree);
                if (TerrainInspectorUtil.PrototypeHasMaterials(terrain.terrainData, selectedTree))
                {
                    int num = 0;
                    TreeInstance instance = new TreeInstance {
                        position = new Vector3(xBase, 0f, yBase),
                        color = GetTreeColor(),
                        lightmapColor = Color.white,
                        prototypeIndex = selectedTree,
                        widthScale = GetTreeWidth(),
                        heightScale = GetTreeHeight()
                    };
                    if (((Event.current.type != EventType.MouseDrag) && (brushSize <= 1f)) || TerrainInspectorUtil.CheckTreeDistance(terrain.terrainData, instance.position, instance.prototypeIndex, spacing))
                    {
                        terrain.AddTreeInstance(instance);
                        num++;
                    }
                    Vector3 prototypeExtent = TerrainInspectorUtil.GetPrototypeExtent(terrain.terrainData, selectedTree);
                    prototypeExtent.y = 0f;
                    float num2 = brushSize / ((prototypeExtent.magnitude * spacing) * 0.5f);
                    int num3 = (int) ((num2 * num2) * 0.5f);
                    num3 = Mathf.Clamp(num3, 0, 100);
                    for (int i = 1; (i < num3) && (num < num3); i++)
                    {
                        Vector2 insideUnitCircle = UnityEngine.Random.insideUnitCircle;
                        insideUnitCircle.x *= brushSize / terrain.terrainData.size.x;
                        insideUnitCircle.y *= brushSize / terrain.terrainData.size.z;
                        Vector3 position = new Vector3(xBase + insideUnitCircle.x, 0f, yBase + insideUnitCircle.y);
                        if (TerrainInspectorUtil.CheckTreeDistance(terrain.terrainData, position, selectedTree, spacing * 0.5f))
                        {
                            instance = new TreeInstance {
                                position = position,
                                color = GetTreeColor(),
                                lightmapColor = Color.white,
                                prototypeIndex = selectedTree,
                                widthScale = GetTreeWidth(),
                                heightScale = GetTreeHeight()
                            };
                            terrain.AddTreeInstance(instance);
                            num++;
                        }
                    }
                }
            }
        }

        public static void RemoveTrees(float xBase, float yBase, bool clearSelectedOnly)
        {
            float radius = brushSize / terrain.terrainData.size.x;
            terrain.RemoveTrees(new Vector2(xBase, yBase), radius, !clearSelectedOnly ? -1 : selectedTree);
        }
    }
}

